package com.hc.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hc.comm.CommData;
import com.hc.service.UserUrlService;
import com.hc.serviceImpl.UserUrlServiceImpl;
import com.hc.util.SystemConfigUtil;

public class UserUrlServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String types = req.getParameter("types");
		if("addUrl".equals(types)){
			addUrl(req, resp);
		}else if ("delUrl".equals(types)) {
			delUserUrl(req, resp);
		}else if ("updateUrl".equals(types)){
			updateUrl(req, resp);
		}else if ("getUrls".equals(types)){
			getUserUrlList(req, resp);
		}else if("getWebTestNumber".equals(types)){
			
			getWebTestNumber(req, resp);
		}else if("updateWebTestNumber".equals(types)){
			
			updateWebTestNumber(req, resp);
		}else if("getDownFileUrl".equals(types)){
			
			getDownFileUrl(req, resp);
		}else if("updateDownFileTestUrl".equals(types)){
			
			updateDownFileTestUrl(req, resp);
		}
	}
	
	private void updateDownFileTestUrl(HttpServletRequest req,
			HttpServletResponse resp) {

		
		String downFileTestUrl = req.getParameter("downFileTestUrl").trim();
		
		
		if(downFileTestUrl!=null&&!"".equals(downFileTestUrl)){
	
			
			CommData.downFileUrl = downFileTestUrl;
			
			SystemConfigUtil.writeConfig();
			
			returnMsg(req, resp, "true");
			
			
		}else {
			
			
			returnMsg(req, resp, "false");
		}
		
	
		
	}

	private void getDownFileUrl(HttpServletRequest req, HttpServletResponse resp) {

	
		
		if(CommData.downFileUrl!=null&&!"".equals(CommData.downFileUrl)){
			String url=CommData.downFileUrl;
		
			url = "\""+url+"\"";
			
			returnMsg(req, resp,url);
		}
		
			
		
	}

	private void updateWebTestNumber(HttpServletRequest req,
			HttpServletResponse resp) {
			
		String webTestNumber = req.getParameter("webTestNumber").trim();
		
		
		if(webTestNumber!=null&&!"".equals(webTestNumber)){
			CommData.webTestNumber = Integer.parseInt(webTestNumber);
			
			SystemConfigUtil.writeConfig();
			
			returnMsg(req, resp, "true");
		}else {
			
			
			returnMsg(req, resp, "false");
		}
		
	}

	private void getWebTestNumber(HttpServletRequest req,
			HttpServletResponse resp) {
		
		returnMsg(req, resp,CommData.webTestNumber+"");
		
	}

	private void addUrl(HttpServletRequest req, HttpServletResponse resp){
		UserUrlService service = new UserUrlServiceImpl();
		returnMsg(req, resp, service.addUrl(req, resp) ? "true" : "false");
	}
	
	private void updateUrl(HttpServletRequest req, HttpServletResponse resp) {
		UserUrlService service = new UserUrlServiceImpl();
		returnMsg(req, resp, service.updateUrl(req, resp) ? "true" : "false");
	}
	
	private void delUserUrl(HttpServletRequest req, HttpServletResponse resp){
		UserUrlService service = new UserUrlServiceImpl();
		returnMsg(req, resp, service.delUrl(req, resp) ? "true" : "false");
	}
	
	private void getUserUrlList(HttpServletRequest req, HttpServletResponse resp) {
		UserUrlService service = new UserUrlServiceImpl();
		returnMsg(req, resp, service.getUrls().toString());
	}
	
	private void returnMsg(HttpServletRequest req, HttpServletResponse resp,
			String msg) {
		resp.setCharacterEncoding("utf-8");

		PrintWriter w;
		try {
			w = resp.getWriter();
			w.write(msg);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	
	}
	
}
